
package com.kugou.game.sdk.ui.dialog;

import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 描述:抽象对话框，项目中所有对话框的父类，统一了对话框样式
 * 
 * @author chenys
 * @since 2013-10-28 下午6:11:44
 */
public abstract class AbsDialog extends BaseSDKDialog implements View.OnClickListener {

    private Activity mActivity;

    private TextView mTitle;

    protected Button mBtnOK, mBtnCancel;

    private ImageView mCloseImg;

    private View mBtnDivider;

    private OnDialogButtonClickListener mOnDialogButtonClickListener;

    protected Bundle mData;

    private View mContentView;

    private WindowManager.LayoutParams lp;

    public AbsDialog(Activity activity) {
        super(activity, Rx.style.kg_user_center_dialog_button);
        mActivity = activity;

        RelativeLayout container = (RelativeLayout) activity.getLayoutInflater().inflate(
                Rx.layout.kg_common_dialog, null);

        mTitle = (TextView) container.findViewById(Rx.id.kg_common_dialog_title_text);
        mBtnOK = (Button) container.findViewById(Rx.id.kg_common_dialog_btn_ok);
        mBtnCancel = (Button) container.findViewById(Rx.id.kg_common_dialog_btn_cancel);

        mBtnDivider = container.findViewById(Rx.id.kg_common_dialog_btn_divider);
        mCloseImg = (ImageView) container.findViewById(Rx.id.kg_common_dialog_close_img);

        mBtnOK.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
        mCloseImg.setOnClickListener(this);

        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        rlp.addRule(RelativeLayout.BELOW, Rx.id.kg_common_diaog_title_bar);
        rlp.addRule(RelativeLayout.ABOVE, Rx.id.kg_common_dialog_bottom_bar);
        mContentView = createContentView();
        container.addView(mContentView, rlp);

        setContentView(container);

        Window dialogWindow = getWindow();
        lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int parentWidth = dm.widthPixels;
        int parentHeight = dm.heightPixels;
        if (parentWidth < parentHeight) {// 竖屏
            lp.width = (int) (parentWidth * 0.9);
            lp.height = (int) (lp.width * 0.64);
        } else {
            lp.width = (int) (parentHeight * 0.9);
            lp.height = (int) (lp.width * 0.64);
        }

        dialogWindow.setAttributes(lp);
    }

    /**
     * 动态设置对话框的大小
     * 
     * @param widthPercent 屏幕总宽的百分比
     * @param heightPercent 屏幕总高的百分比
     */
    public void setWindowSize(double widthPercent, double heightPercent) {
        DisplayMetrics dm = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int parentWidth = dm.widthPixels;
        int parentHeight = dm.heightPixels;
        if (parentWidth < parentHeight) {// 竖屏
            lp.width = (int) (parentWidth * widthPercent);
            lp.height = (int) (lp.width * heightPercent);
        } else {
            lp.width = (int) (parentHeight * widthPercent);
            lp.height = (int) (lp.width * heightPercent);
        }

        getWindow().setAttributes(lp);
    }

    /**
     * 创建显示内容的view
     * 
     * @return
     */
    protected abstract View createContentView();

    public Activity getActivity() {
        return mActivity;
    }

    /**
     * 获取显示内容的view
     * 
     * @return
     */
    public View getContentView() {
        return mContentView;
    }

    public LayoutInflater getLayoutInflater() {
        return (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * 保存一些交互数据
     * 
     * @param data
     */
    public void setData(Bundle data) {
        mData = data;
    }

    public Bundle getData() {
        return mData;
    }

    public void setOnDialogButtonClickListener(OnDialogButtonClickListener listener) {
        mOnDialogButtonClickListener = listener;
    }

    /**
     * 设置对话框标题
     * 
     * @param title
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /**
     * 设置标题文字的位置
     * 
     * @param gravity
     */
    public void setTitleGravity(int gravity) {
        mTitle.setGravity(gravity);
    }

    /**
     * 设置确定按钮显示文字
     * 
     * @param okText
     */
    public void setOkBtnText(String okText) {
        mBtnOK.setText(okText);
    }

    /**
     * 设置取消按钮显示文字
     * 
     * @param cancelText
     */
    public void setCancelBtnText(String cancelText) {
        mBtnCancel.setText(cancelText);
    }

    /**
     * 设置OK按钮是否显示
     * 
     * @param visible
     */
    public void setOkBtnVisible(boolean visible) {
        mBtnOK.setVisibility(visible ? View.VISIBLE : View.GONE);
        mBtnDivider.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置Cancel按钮是否显示
     * 
     * @param visible
     */
    public void setCancelBtnVisible(boolean visible) {
        mBtnCancel.setVisibility(visible ? View.VISIBLE : View.GONE);
        mBtnDivider.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置对话框关闭按钮是否可见
     * 
     * @param visible
     */
    public void setCloseImgVisibility(boolean visible) {
        mCloseImg.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == Rx.id.kg_common_dialog_btn_ok) {
            if (mOnDialogButtonClickListener != null) {
                mOnDialogButtonClickListener.onOkBtnClick();
            }
            dismiss();
        } else if (id == Rx.id.kg_common_dialog_btn_cancel) {
            if (mOnDialogButtonClickListener != null) {
                mOnDialogButtonClickListener.onCancelBtnClick();
            }
            dismiss();
        } else if (id == Rx.id.kg_common_dialog_close_img) {
            dismiss();
        }
    }

    @Override
    public void show() {
        if (getActivity() != null && !getActivity().isFinishing()) {
            super.show();
        }
    }

    public interface OnDialogButtonClickListener {

        void onOkBtnClick();

        void onCancelBtnClick();

    }

}
